<?php
//文件上传
function uploadFile($filename,$path="") {
	if(empty ($path)) {
		$attachment_dir ="attach/upload/" .date('Ym')."/";
	}else {
		$attachment_dir ="attach/upload/$path/" .date('Ym')."/";
	}
	!is_dir(BASE_URI."/".$attachment_dir)&&mk_dir(BASE_URI."/".$attachment_dir);
	$AllowedExtensions = array('bmp','gif','jpeg','jpg','png','doc','rar','zip','docx','xls','xlsx');
	$Extensions = end(explode(".",$_FILES[$filename]['name']));

	if(!in_array(strtolower($Extensions),$AllowedExtensions)) {
		exit("<script>alert('格式错误！);window.history.go(-1)</script>");
	}


	$file_name = date('YmdHis').'_'.rand(10,999).'.'.$Extensions;
	$upload_file = $attachment_dir.$file_name;
	$upload_absolute_file = BASE_URI."/".$upload_file;
	if (move_uploaded_file($_FILES[$filename]['tmp_name'], $upload_absolute_file)) {
		$record = array(
                'filename'			=>$file_name,
                'ffilename'			=>$_FILES [$filename]['name'],
                'path'				=>$upload_file,
                'ext'				=>$Extensions,
                'size'				=>$_FILES [$filename]['size'],
                'upload_date'		=>date("Y-m-d H:i:s")
		);
		return $upload_file;
	}else {
		return false;
	}
}

function getCategory($pid=0,$level=0,$curId="") {
	for($lev = 0; $lev < $level; $lev ++) {
		$level_nbsp .= "&nbsp;&nbsp;";
	}
	$level++;
	$db=new csyr("category");
	$cates=$db->where("pid=".$pid)->findAll();
	$c="";
	if(is_array($cates)) {
		foreach ($cates as $val) {
			if($curId!="" && $val['id']==$curId) {
				$selected="selected='selected'";
			}else{
				$selected="";
			}
			$c.="<option $selected value='".$val['id']."'>".$level_nbsp.$val['name']."</option> \n";
			$c.=getCategory($val['id'],$level,$curId);
		}
	}
	return $c;
}

function getAllCategory($cId=0) {
	$c="<select name='categoryid' id='categoryid'>";
	$c.=getCategory(0,0,$cId);
	$c.="</select>";
	return $c;
}

function getCurCategory($id) {
	$db=new csyr("category");
	$pid=$db->find($id);
	$pname=$pid['name'].' > ';
	if($pid['pid']!=0) {
		$pname=getCurCategory($pid['pid']).$pname;
	}
	return $pname;
}

//直系子类
function getSubCategory($pid,$curId=null) {
	$db=new csyr("category");
	$subCate=$db->where("pid=$pid")->findAll();
	if(is_array($subCate)) {
		$c="<select name='categoryid' id='categoryid'>";
		foreach($subCate as $v){
			if($curId==$v['id']){
				$select="selected='selected'";
			}else{
				$select='';
			}
			$c.="<option $select value='".$v['id']."'>".$v['name']."</option>";
		}
		$c.="</select>";
		return $c;
	}else {
		return false;
	}
}
//所有子类
function getAllSubCategory($pid,$curId=0) {
	$c="<select name='categoryid' id='categoryid'>";
	$c.=getCategory($pid,0,$curId);
	$c.="</select>";
	return $c;
}
//商家下拉框
function getSellerSelect($selected=0){
	$db=new csyr("seller");
	$select="<select name='storeid' id='storeid'>";
	$sellerArr=$db->group("name")->findAll();
	foreach($sellerArr as $val){
		if($val['id']==$selected){
			$curSelect="selected='selected'";
		}else{
			$curSelect="";
		}
		$select.="<option $curSelect value='".$val["id"]."'>".$val['name']."</option> \n";
	}
	$select.="</select>";
	return $select;
}

//广告位
function getAdPosition(){
	$p=array(
        "导航-全局文字广告-左"=>'global|word|1',
        "导航-全局文字广告-右"=>'global|word|2',
        "首页底部图片广告"=>"index.php|global|banner|bottom|1",
        "秒杀专区底部图片广告"=>"killing-zone.php|global|banner|bottom|1",
        "秒杀专区右侧图片广告"=>"killing-zone.php|global|right|1",
        "团购专区底部图片广告"=>"group-zone.php|global|banner|bottom|1",
        "团购专区右侧图片广告"=>"group-zone.php|global|right|1",
        "竞拍专区底部图片广告"=>"auction-zone.php|global|banner|bottom|1",
        "竞拍专区右侧图片广告"=>"auction-zone.php|global|right|1",
        "商家专区底部图片广告"=>"client.php|banner|bottom|1",
        "商家专区右侧图片广告"=>"client.php|right|1",
        "MSN-TAB右下侧图片广告"=>"osTab.php|banner|bottom|1"
        );

        return $p;
}

//保存标签
function saveTag($tagname,$position){
	if(empty($tagname) || empty($position)) return false;
	$dbTag=new csyr("tag");
	$tags=$dbTag->where("position='$position'")->find();
	if(is_array($tags)){
		$dbTag->where("position='$position'")->save(array('name'=>$tagname));
		return true;
	}else{
		if( $id=$dbTag->add(array('name'=>$tagname,'position'=>"$position")) )
			return true;
		else
			return false;
	}
}
